#include "SoundTask.h"
const float DISTANCEFACTOR = 1.0f;  //Units per meter.  I.e feet would = 3.28.  centimeters would = 100.

SoundTask::SoundTask(void){
	eventsystem = 0;

	shutDown = false;
}

SoundTask::~SoundTask(void){
	FMOD_RESULT result;
	if(eventsystem != 0)
    result = eventsystem->release();
    ERRCHECK(result);
}

bool SoundTask::execute(double delta){
		
	if(eventsystem != 0)
		eventsystem->update();

	return true;
}

void SoundTask::keyDown(const InputEvent& e)
{
	switch(e.id) {
	case '1':
		soundEvent[WALK_GRAVEL]->stop();
		soundEvent[WALK_GRAVEL]->start();
		break;
	case '2':
		soundEvent[SWORD]->stop();
		soundEvent[SWORD]->start();
		break;
	case '3':
		soundEvent[COLLISION]->stop();
		soundEvent[COLLISION]->start();
		break;
	case '4':
		soundEvent[EXPLOSION]->stop();
		soundEvent[EXPLOSION]->start();
		break;
	case '5':
		cycle->keyOff();
		break;
	}
}

void SoundTask::init(){
	FMOD_RESULT result;
	FMOD_MUSIC_ITERATOR cueIterator;

	result = FMOD::EventSystem_Create(&eventsystem);
	ERRCHECK(result);

	result = eventsystem->init(256, FMOD_INIT_NORMAL, 0);
	ERRCHECK(result);

	result = eventsystem->load("./sounds/scene_fmod.fev",0,&eventproject);
	ERRCHECK(result);

	//result = eventsystem->getMusicSystem(&musicsystem);
	//ERRCHECK(result);

	//result = musicsystem->setVolume(0.4f);
	//ERRCHECK(result);

	//result = musicsystem->getCues(&cueIterator, "music");
	//ERRCHECK(result);

	//result = musicsystem->prepareCue(cueIterator.value->id, &musicPrompt);
	//ERRCHECK(result);

	FMOD::EventGroup* evtgroup;
	result = eventproject->getGroup("untitled", true, &evtgroup);
	ERRCHECK(result);

	result = evtgroup->getEvent("walk_gravel", FMOD_EVENT_DEFAULT, &soundEvent[WALK_GRAVEL]);
	ERRCHECK(result);

	result = evtgroup->getEvent("attack", FMOD_EVENT_DEFAULT, &soundEvent[SWORD]);
	ERRCHECK(result);

	result = evtgroup->getEvent("impact", FMOD_EVENT_DEFAULT, &soundEvent[COLLISION]);
	ERRCHECK(result);

	result = evtgroup->getEvent("explosion", FMOD_EVENT_DEFAULT, &soundEvent[EXPLOSION]);
	ERRCHECK(result);

	result = soundEvent[EXPLOSION]->getParameter("Cycle", &cycle);
	
}

void SoundTask::launchSound(char* sound){
	soundEvent[SoundEvent::SWORD]->start();
}


